seajs.use(['formValidator', 'validation-utils', 'authcode-ui'], function(MyFormValidator, ValidationUtils, AuthCodeUI) {
	$(document).ready(function($) {
		jQuery.formValidator.initConfig({
			validatorgroup: "loginForm",
			formid: "loginForm",
			onsuccess: ajaxSubmitForm,
			onerror: function(msg) {}
		});
		$("#loginForm_lognumber").formValidator({
			validatorgroup: "loginForm",
			tipid: "tip_loginForm",
			onshow: '',
			onfocus: '',
			oncorrect: ''
		}).functionValidator({
			fun: validateLoginAcco
		})
		$("#loginForm_lognumber").keyup(function() {
			var str = this.value;
			var result = "";
			var flag = false;
			for(var i = 0; i < str.length; i++) {
				if(str.charCodeAt(i) == 12288) {
					flag = true;
					result += String.fromCharCode(str.charCodeAt(i) - 12256);
					continue;
				}
				if(str.charCodeAt(i) > 65280 && str.charCodeAt(i) < 65375) {
					flag = true;
					result += String.fromCharCode(str.charCodeAt(i) - 65248);
				} else {
					result += String.fromCharCode(str.charCodeAt(i));
				}
			}
			if(flag) {
				this.value = result;
			}
		});
		$("#loginForm_logpassword").formValidator({
			validatorgroup: "loginForm",
			tipid: "tip_loginForm",
			onshow: '',
			onfocus: '',
			oncorrect: ''
		}).inputValidator({
			min: 1,
			empty: {
				leftempty: false,
				rightempty: false,
				emptyerror: "请输入您的密码"
			},
			onerror: "请输入您的密码"
		}).regexValidator({
			regexp: "^([a-zA-Z0-9*\\u0000-\\u00FF]{6,20})$",
			onerror: "密码输入不规范"
		})
		$("#loginForm_logpassword").keyup(function() {
			var str = this.value;
			var result = "";
			var flag = false;
			for(var i = 0; i < str.length; i++) {
				if(str.charCodeAt(i) == 12288) {
					flag = true;
					result += String.fromCharCode(str.charCodeAt(i) - 12256);
					continue;
				}
				if(str.charCodeAt(i) > 65280 && str.charCodeAt(i) < 65375) {
					flag = true;
					result += String.fromCharCode(str.charCodeAt(i) - 65248);
				} else {
					result += String.fromCharCode(str.charCodeAt(i));
				}
			}
			if(flag) {
				this.value = result;
			}
		});
		$("#loginForm_authcode").formValidator({
			validatorgroup: "loginForm",
			tipid: "tip_loginForm",
			onshow: '',
			onfocus: '',
			oncorrect: ''
		}).functionValidator({
			fun: function(val, elem) {
				try {
					if(!AuthCodeUI.needAuthCode && val == '') {
						return true;
					}
					if(val == '') {
						return '请输入验证码';
					}
					return true;
				} catch(e) {
					return '';
				}
			}
		})

		$("#loginForm_authcode").keyup(function() {
			var str = this.value;
			var result = "";
			var flag = false;
			for(var i = 0; i < str.length; i++) {
				if(str.charCodeAt(i) == 12288) {
					flag = true;
					result += String.fromCharCode(str.charCodeAt(i) - 12256);
					continue;
				}
				if(str.charCodeAt(i) > 65280 && str.charCodeAt(i) < 65375) {
					flag = true;
					result += String.fromCharCode(str.charCodeAt(i) - 65248);
				} else {
					result += String.fromCharCode(str.charCodeAt(i));
				}
			}
			if(flag) {
				this.value = result;
			}
		});

		function validateLoginAcco(val, elem) {
			if($("#loginForm_certificatetype").val() == "0") {
				var val = $("#loginForm_lognumber").val();
				if(val == null || val.length == 0) return ValidationUtils.getMessage('请输入{0}', new Array('登录账号'));
				if(val.length == 15 || val.length == 18) {
					reg = /^(\d{15}|\d{18}|\d{17}[a-zA-Z]{1})$/;
				} else if(val.length <= 11) {
					reg = /^(13|14|15|16|18|17|19)[0-9]{9}$/;
				}
				if(!reg.test(val)) return "手机号码/身份证号号码不合规范，请重新输入！";
				var result = true;
				if(val.length == 15 || val.length == 18) {
					result = ValidationUtils.isCardID(val) == true ? true : false;
				}
				if(!result) return "手机号码/身份证号号码不合规范，请重新输入！";
			} else if($("#loginForm_certificatetype").val() == "00") {
				var val = $("#loginForm_lognumber").val();
				if(val == null || val.length == 0) return ValidationUtils.getMessage('请输入{0}', new Array('登录账号'));
				reg = /^(\d{12}|[\*]{1}[0-9]{11}|[0-9][0-9][a-zA-Z][0-9]{9}|\d{10}|[\*]{1}[0-9a-zA-Z]{9}|[0-9a-zA-Z]{10}|[0-9][a-zA-Z]\d{10})$/;
				if(!reg.test(val)) return "基金账号号码不合规范，请重新输入！";
			} else if($("#loginForm_certificatetype").val() == "N") {
				var val = $("#loginForm_lognumber").val();
				if(val == null || val.length == 0) return ValidationUtils.getMessage('请输入{0}', new Array('登录账号'));
				reg = /^.{1,20}$/;
				if(!reg.test(val)) return "基金客户昵称号码不合规范，请重新输入！";
			} else if($("#loginForm_certificatetype").val() == "1") {
				var val = $("#loginForm_lognumber").val();
				if(val == null || val.length == 0) return ValidationUtils.getMessage('请输入{0}', new Array('登录账号'));
				reg = /^([\u4e00-\u9fa5\w\d\-\(\)\ï¼\ï¼)\/\_\[\]\#]{3,})$/;
				if(!reg.test(val)) return "中国护照号码不合规范，请重新输入！";
			} else if($("#loginForm_certificatetype").val() == "2") {
				var val = $("#loginForm_lognumber").val();
				if(val == null || val.length == 0) return ValidationUtils.getMessage('请输入{0}', new Array('登录账号'));
				reg = /^([\u4e00-\u9fa5\w\d\-\(\)\ï¼\ï¼)\/\_\[\]\#]{4,})$/;
				if(!reg.test(val)) return "军官证号码不合规范，请重新输入！";
			} else if($("#loginForm_certificatetype").val() == "3") {
				var val = $("#loginForm_lognumber").val();
				if(val == null || val.length == 0) return ValidationUtils.getMessage('请输入{0}', new Array('登录账号'));
				reg = /^([\u4e00-\u9fa5\w\d\-\(\)\ï¼\ï¼)\/\_\[\]\#]{3,})$/;
				if(!reg.test(val)) return "士兵证号码不合规范，请重新输入！";
			} else if($("#loginForm_certificatetype").val() == "5") {
				var val = $("#loginForm_lognumber").val();
				if(val == null || val.length == 0) return ValidationUtils.getMessage('请输入{0}', new Array('登录账号'));
				reg = /^([\u4e00-\u9fa5\w\d\-\(\)\ï¼\ï¼)\/\_\[\]\#]{3,})$/;
				if(!reg.test(val)) return "出生证/户口本号码不合规范，请重新输入！";
			} else if($("#loginForm_certificatetype").val() == "7") {
				var val = $("#loginForm_lognumber").val();
				if(val == null || val.length == 0) return ValidationUtils.getMessage('请输入{0}', new Array('登录账号'));
				reg = /^([\u4e00-\u9fa5\w\d\-\(\)\ï¼\ï¼)\/\_\[\]\#]{3,})$/;
				if(!reg.test(val)) return "其他号码不合规范，请重新输入！";
			} else if($("#loginForm_certificatetype").val() == "8") {
				var val = $("#loginForm_lognumber").val();
				if(val == null || val.length == 0) return ValidationUtils.getMessage('请输入{0}', new Array('登录账号'));
				reg = /^([\u4e00-\u9fa5\w\d\-\(\)\ï¼\ï¼)\/\_\[\]\#]{3,})$/;
				if(!reg.test(val)) return "文职证号码不合规范，请重新输入！";
			} else if($("#loginForm_certificatetype").val() == "9") {
				var val = $("#loginForm_lognumber").val();
				if(val == null || val.length == 0) return ValidationUtils.getMessage('请输入{0}', new Array('登录账号'));
				reg = /^([\u4e00-\u9fa5\w\d\-\(\)\ï¼\ï¼)\/\_\[\]\#]{3,})$/;
				if(!reg.test(val)) return "警官证号码不合规范，请重新输入！";
			}
			return true;
		};

		var options = {
			dataType: "json",
			beforeSubmit: processRequest,
			success: processResponse,
			error: processError
		};

		function ajaxSubmitForm() {
			$('#loginForm').ajaxSubmit(options);
			return false;
		}

		function processRequest(formData, jqForm, options) {
			var submitId = "loginForm_submit";
			var submitCtrl = $("#" + submitId);
			if(submitCtrl.length > 0) {
				if(submitCtrl.is('a')) {
					submitCtrl.attr('href', 'javascript:void(0)');
				} else {
					submitCtrl.attr("disabled", "disabled");
				}
				submitCtrl.css("color", "#cccccc");
			}
			$('#tip_loginForm').html("数据提交中，请稍候...").removeClass().show().addClass("onLoad");
		}

		function processResponse(responseText, statusText) {
			if(!responseText.success) {
				$('#tip_loginForm').html(responseText.errormessage).removeClass("onError").show().addClass("onError");
				if(responseText.tourl && responseText.tourl != '') {
					if(responseText.target == 'topmost') {
						top.location = responseText.tourl;
					}
					window.document.location.href = responseText.tourl;
					return;
				}
				var submitId = "loginForm_submit";
				var submitCtrl = $("#" + submitId);
				if(submitCtrl.length > 0) {
					if(submitCtrl.is('a')) {
						submitCtrl.attr('href', 'javascript:$("#loginForm").submit()');
					} else {
						submitCtrl.removeAttr("disabled");
					}
					submitCtrl.css("color", "");
				}
				$("#loginForm_logpassword").val("");
				$("#loginForm_authcode").val("");
				$("#loginForm_img_authcode").attr({
					src: '/new/authcode.jpg?t=' + new Date().getTime()
				});
				try {
					AuthCodeUI.checkShowAuthcode(authcodeCallback)
				} catch(e) {}
			} else {
				if(responseText.topost == "true") {
					topostForm(responseText.tourl, responseText.target, responseText);
				} else if(responseText.target == 'parent') {
					parent.window.document.location.href = responseText.tourl;
				} else if(responseText.target == 'top') {
					window.top.document.location.href = responseText.tourl;
				} else {
					window.document.location.href = responseText.tourl;
				}
			}
		}

		function processError(jqXHR, statusText, errorThrown) {
			var submitId = "loginForm_submit";
			var submitCtrl = $("#" + submitId);
			if(submitCtrl.length > 0) {
				if(submitCtrl.is('a')) {
					submitCtrl.attr('href', 'javascript:$("#loginForm").submit()');
				} else {
					submitCtrl.removeAttr("disabled");
				}
				submitCtrl.css("color", "");
			}
		}
	});

});